<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品介绍" prop="goodsInfo">
        <el-input
          v-model="queryParams.goodsInfo"
          placeholder="请输入商品介绍"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品价格" prop="goodsPrice">
        <el-input
          v-model="queryParams.goodsPrice"
          placeholder="请输入商品价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家外键id" prop="goodsShopId" v-show="false">
        <el-input
          v-model="queryParams.goodsShopId"
          placeholder="请输入商家外键id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品库存" prop="goodsNum" v-show="false">
        <el-input
          v-model="queryParams.goodsNum"
          placeholder="请输入商品库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="整数" prop="goodsNumber" v-show="false">
        <el-input
          v-model="queryParams.goodsNumber"
          placeholder="请输入整数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小数" prop="goodsDecimal" v-show="false">
        <el-input
          v-model="queryParams.goodsDecimal"
          placeholder="请输入小数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goods:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goods:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品介绍" align="center" prop="goodsInfo" />
      <el-table-column label="商品价格" align="center" prop="goodsPrice" />
      <el-table-column label="商品图片" align="center" prop="goodsImgId" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.images.imagesUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商品库存" align="center" prop="goodsNum" />
      <el-table-column label="商品状态" align="center" prop="goodsStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.goodsStatus === 1">上架</span>
          <span v-if="scope.row.goodsStatus === 2">下架</span>
        </template>
      </el-table-column>
      <el-table-column label="所属分类" align="center" prop="category.categoryName" />
      <el-table-column label="整数" align="center" prop="goodsNumber" />
      <el-table-column label="小数" align="center" prop="goodsDecimal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品介绍" prop="goodsInfo">
          <el-input v-model="form.goodsInfo" placeholder="请输入商品介绍" />
        </el-form-item>
        <el-form-item label="商品价格" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="商品图片路径" prop="images.imagesUrl">
          <image-upload v-model="form.images.imagesUrl"/>
        </el-form-item>
<!--        <template>-->
        <el-form-item label="所属分类" prop="goodsCategoryId">
          <el-cascader
            v-model="form.goodsCategoryId"
            :options="options"
            :props="cascaderProps"
            change-on-select
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
<!--        </template>-->
        <el-form-item label="商家外键id" prop="goodsShopId" v-show="false">
          <el-input v-model="form.goodsShopId" placeholder="请输入商家外键id" />
        </el-form-item>
        <el-form-item label="商品库存" prop="goodsNum">
          <el-input v-model="form.goodsNum" placeholder="请输入商品库存" />
        </el-form-item>
        <el-form-item label="整数" prop="goodsNumber">
          <el-input v-model="form.goodsNumber" placeholder="请输入整数" />
        </el-form-item>
        <el-form-item label="小数" prop="goodsDecimal">
          <el-input v-model="form.goodsDecimal" placeholder="请输入小数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods, classifyList} from "@/api/goods/goods";

export default {
  name: "Goods",
  data() {
    return {
      categoryLists:[],
      options: [
        {
          value: null,
          label: null,
          children: [
            {
              value:null,
              label:null
            }
          ]
        },
      ],
      cascaderProps: {
        checkStrictly: true, // 允许选择任何一级的选项
        value: 'value',
        label: 'label',
        children: 'children'
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        goodsInfo: null,
        goodsPrice: null,
        goodsImgId: null,
        goodsShopId: null,
        goodsNum: null,
        goodsStatus: null,
        goodsCategoryId: [],
        goodsNumber: null,
        goodsDecimal: null
      },

      // 表单参数
      form: {
        images:{
          imagesUrl:undefined,
          imagesType:null,
        },
        selectedValue:null,
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleChange(value) {
      this.form.goodsCategoryId = value[value.length-1];
      console.log(value);
    },
    /** 查询商品列表列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        console.log("goodsList",response.rows)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        goodsName: null,
        goodsInfo: null,
        goodsPrice: null,
        goodsImgId: null,
        goodsShopId: null,
        goodsNum: null,
        goodsStatus: null,
        goodsCategoryId: [],
        goodsNumber: null,
        goodsDecimal: null,
        images:{
          imagesUrl:null
        },
        selectedValue:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      classifyList().then(response => {
        this.categoryLists = response.rows;
        this.options = []; // 清空或重新初始化options
        this.categoryLists.forEach(category => {
          let option = {
            value: category.categoryId,
            label: category.categoryName,
            children: []
          };
          if (category.categoryParentLevel === 0) {
            this.options.push(option);
          } else {
            const parent = this.options.find(o => o.value === category.categoryParentLevel);
            if (parent) {
              let childOption = {
                value: category.categoryId,
                label: category.categoryName
              };
              parent.children.push(childOption);
            }
          }
        });
      });
      this.reset();
      this.open = true;
      this.title = "添加商品列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      classifyList().then(response => {
        this.categoryLists = response.rows;
        this.options = []; // 清空或重新初始化options
        this.categoryLists.forEach(category => {
          let option = {
            value: category.categoryId,
            label: category.categoryName,
            children: []
          };
          if (category.categoryParentLevel === 0) {
            this.options.push(option);
          } else {
            const parent = this.options.find(o => o.value === category.categoryParentLevel);
            if (parent) {
              let childOption = {
                value: category.categoryId,
                label: category.categoryName
              };
              parent.children.push(childOption);
            }
          }
        });
      });
      this.reset();
      const goodsId = row.goodsId || this.ids
      getGoods(goodsId).then(response => {
        this.form = response.data;
        console.log("handleUpdateform：",response.data)
        this.open = true;
        this.title = "修改商品列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != null) {
            updateGoods(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$modal.confirm('是否确认删除商品列表编号为"' + goodsIds + '"的数据项？').then(function() {
        return delGoods(goodsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goods/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.el-radio__original {
  display: none !important; /* 隐藏原生 radio 输入，但仍然允许交互 */
}
</style>
